import {
  HttpApi
} from '../model/index.js'
const httpApi = new HttpApi

Page({
  // onShow() {
  //   wx.reportAnalytics('enter_home_programmatically', {})
  // },
  // onShareAppMessage() {
  //   return {
  //     title: '小程序官方组件展示',
  //     path: 'page/component/index'
  //   }
  // },

  data: {
    windowHeight: 100,
    aqkcHeight: 100,
    activeTab: 'homeTab',
    banner_arry: [],
    todayTouFang: 0,
    totalTouFang: 0,
    myJiFen: 0,
    maxPoint: 0,
    start: 1,
    size: 300,
    loading: false,
    lng: "",
    lat: "",
    records: [],
    articleRecords: [],
    showQRCode: false,
    hiddenName: false,
    qrcodeImgurl: "",
    showNullImg: false,
    userinfo: {
      score: "",
      feedbackNum: 1,
      nikeName: "",
      communityId: "",
      communityName: "",
      userId: "",
    },
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    id: 1,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var login = wx.getStorageSync('login')
    if (login == 'NO') {
      that.setData({
        hiddenName: false,
      })
    } else if (login == 'OK') {
      that.setData({
        hiddenName: true,
      })
      that.getDustbin();
      that.article();
    }

    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    //设置scroll-view高度
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowHeight: res.windowHeight - 80
        });
        that.setData({
          aqkcHeight: res.windowHeight
        });
        console.log(res.windowHeight)
      }
    });
  
  },
  onShow: function(options) {
    var _this = this
    var login = wx.getStorageSync('login')
    if (login == 'NO') {
      _this.onLoad()
    }
  },
  article(){
    var _this = this
    httpApi.article().then(res => {
      _this.setData({
        articleRecords: res.rows,
      })
    })
  },
  getDustbin() {
    this.loading = true
    var _this = this
    httpApi.course().then(res => {
      _this.setData({
        records: res.rows,
        loading: false
      })
      if (_this.data.records.length == 0) {
        _this.setData({
          showNullImg: true,
        })
      }
    })
  },
  // 拨打客服电话
  customerTel() {
    wx.makePhoneCall({
      phoneNumber: '4000000000'
    })
  },
  login() {
    wx.navigateTo({
      url: 'pages/login/login'
    });
  },
  register() {
    wx.navigateTo({
      url: 'pages/register/register'
    });
  },
  closeQR() {
    this.setData({
      showQRCode: false
    })
  },
  showQR() {
    this.setData({
      showQRCode: true
    })
    // var _this = this
    // httpApi.qrcode().then(res => {
    // 	if (res.code == 0) {
    // 		_this.setData({
    // 			showQRCode:true
    // 		})
    // 	}
    // })
  },
  takePhoto() {
    if (this.data.maxPoint >= 2) {
      wx.showModal({
        title: '提示',
        content: '拍照上传每日上限2积分'

      })
      return ""
    }
    wx.navigateTo({
      url: "./pages/open_camera/open_camera"
    })
  },
  addOrder() {
    wx.navigateTo({
      url: "./pages/book/book"
    })
  },
  goContent(e) {

  },
  goMap(e) {
    var latitude = e.currentTarget.dataset.lat;
    var longitude = e.currentTarget.dataset.lng;
    var distance = e.currentTarget.dataset.distance;
    if (distance == null) {
      wx.showModal({
        title: '提示',
        content: '回收箱未填写经纬度无法定位,请联系管理员处理',
        success: function(res) {}
      })
      return;
    }
    wx.navigateTo({
      url: "./pages/nearbyRecyclingMachine/nearbyRecyclingDetail/nearbyRecyclingDetail?latitude=" + JSON.stringify(latitude) + "&longitude=" + JSON.stringify(longitude),
    })
  },
  kindToggle(e) {
    const module = e.currentTarget.dataset.s;
    if (module == 'yuyue') {
      wx.navigateTo({
        url: 'pages/appointmentRecord/appointmentRecord'
      });
      return;
    } else if (module == 'putRecord') {
      wx.navigateTo({
        url: 'pages/putRecord/putRecord'
      });
      return;
    } else if (module == 'exchangeRecord') {
      wx.navigateTo({
        url: 'pages/exchangeRecord/exchangeRecord'
      });
      return;
    } else if (module == 'addressManagement') {
      wx.showToast({
        title: "功能开发中...",
        icon: "none"
      })
      //wx.navigateTo({ url: 'pages/addressManagement/addressManagement' });
      return;
    } else if (module == 'addAddress') {
      wx.navigateTo({
        url: 'pages/addAddress/addAddress'
      });
      return;
    } else if (module == 'bindQRCode') {
      wx.showToast({
        title: "功能开发中...",
        icon: "none"
      })
      //wx.navigateTo({ url: 'pages/bindQRCode/bindQRCode' });
      return;
    } else if (module == 'feedback') {
      wx.navigateTo({
        url: 'pages/feedback/feedback'
      });
      return;
    } else if (module == 'cashOut') {
      wx.showToast({
        title: "功能开发中...",
        icon: "none"
      })
      //wx.navigateTo({ url: 'pages/cashOut/cashOut' });
      return;
    } else if (module == 'setting') {
      wx.navigateTo({
        url: 'pages/setting/setting'
      });
      return;
    } else if (module == 'about') {
      wx.navigateTo({
        url: 'pages/about/about'
      });
      return;
    } else if (module == 'deliveryGuide') {
      wx.navigateTo({
        url: 'pages/deliveryGuide/deliveryGuide'
      });
      return;
    } else if (module == 'touFangJiLu') {
      wx.navigateTo({
        url: 'pages/touFangJiLu/touFangJiLu'
      });
      return;
    } else if (module == 'hsfl') {
      wx.navigateTo({
        url: 'pages/recoveryClassify/recoveryClassify'
      });
      return;
    } else if (module == 'hsj') {
      wx.navigateTo({
        url: 'pages/nearbyRecyclingMachine/nearbyRecyclingMachine'
      });
      return;
    }
  },
  // 切换tab
  tabChange(event) {
    var that = this;
    let str = event.currentTarget.dataset.s;
    this.setData({
      activeTab: str
    });
    if (str == 'homeTab') {
      wx.setNavigationBarTitle({
        title: '首页'
      });
    } else if (str == 'myTab') {
      wx.setNavigationBarTitle({
        title: '我的'
      });
    } else if (str == 'inform') {
      wx.setNavigationBarTitle({
        title: '通知'
      });
    }
  },

  //  tab切换逻辑
  swichNav: function(e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  bindChange: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },

  goContent: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var arr = that.data.articleRecords[index];
    var catalogTreeName = arr.catalogTreeName;
    var name = arr.name;
    var content = arr.content;
    wx.navigateTo({
      url: './aqcs/aqcs?catalogTreeName=' + catalogTreeName + "&name=" + name + "&content=" + content
    });
    return;
  },

  goYhcl: function(e) {
    wx.navigateTo({
      url: 'pages/appointmentRecord/appointmentRecord'
    });
    return;
  },

  goInform: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: './inform/inform?id=' + id
    });
    return;
  }
})